using System;
using System.Collections.Generic;
using System.Windows;
using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Unity;
using Shatkotha.WPF.Global.Bases;
using Shatkotha.WPF.Global.Common.Events;
using Shatkotha.WPF.Global.Data;
using Shatkotha.WPF.Global.Data.BLL;
using Shatkotha.WPF.Global.Modules.Contacts;
using System.Collections.ObjectModel;

namespace Shatkotha.WPF.Contacts.Presenters
{
    public class ContactListPresenter : APresenter, IContactListPresenter
    {
        public ObservableCollection<IPerson> PersonList
        {
            get { return (ObservableCollection<IPerson>)GetValue(PersonListProperty); }
            set { SetValue(PersonListProperty, value); }
        }

        public IUnityContainer Container { get; set; }

        public DependencyProperty PersonListProperty = DependencyProperty.Register("PersonListProperty", typeof(ObservableCollection<IPerson>), typeof(ContactListPresenter));

        public ContactListPresenter(IContactListView view,IUnityContainer container,IEventAggregator eventAggregator)
            : base(view)
        {
            Container = container;
            GetAllPerson();
            eventAggregator.GetEvent<PersonAddedEvent>().Subscribe(PersonAddedEventHanlder);
        }

        private void GetAllPerson()
        {
            var personManager = Container.Resolve<IPersonManager>();
            PersonList = personManager.GetAllPerson();
        }

        private void PersonAddedEventHanlder(object obj)
        {
            GetAllPerson();
        }
    }
}